<script type="text/javascript">
var fade_delay = 200;
var restaurant_name;
var restaurant_address;
var restaurants_database;
var search_box = '<input id="search_text"/><br/>Fastfood <input type="checkbox" id="search_cb_fastfood"/>Vegetarian <input type="checkbox" id="search_cb_vegetarian"/>Take-Away <input type="checkbox" id="search_cb_takeaway"/>Alcohol <input type="checkbox" id="search_cb_alcohol"/><br/><hr/><br/><h1>Restaurant List</h1>';

$('#search_text').select();
	
function update_restaurant_list(_value)
{
	$.ajax({
		url: "<?php echo site_url('restaurant/get_json_restaurants');?>",
  		type: 'POST',
		dataType: "json",
        
        success: function (msg) {
        	update_restaurant_list_filter(msg);
        }
    });
}

function update_restaurant_list_filter(data)
{
	//initialize variables and fetch values
	var _array = data;
	//var form_data = {};
	
	filter_text = $('input#search_text').val();
	filter_fastfood = $('input#search_cb_fastfood').val();
	filter_vegetarian = $('input#search_cb_vegetarian').val();
	filter_takeaway = $('input#search_cb_takeaway').val();
	filter_alcohol = $('input#search_cb_alcohol').val();
	

	$('#browse_restaurants_body').empty();
	for(var i = 0; i<_array.length; i++)
	{
		//search_field search
		var reg_expr = new RegExp(filter_text, 'i');
		var search_text = reg_expr.test(_array[i]['name'].toString());

		if(search_text == true)
		{
			//checkboxes search
			var checkbox_checks = new Array(filter_fastfood, filter_vegetarian, filter_takeaway, filter_alcohol);
			var checkbox_checksum = 0;
			for(var k = 0; k < 4; k++)//sum the checkbox_checks to get a checksum
			{
				checkbox_checksum = checkbox_checksum + parseInt(checkbox_checks[k]);
			}
	
			var checkbox_matched = false;
			var checkbox_match = 0;
			for(var k = 0; k < 4; k++)
			{
				if(parseInt(checkbox_checks[k]) == 1)
				{
					if(k == 0 && _array[i]['fastfood'] == 1)
						checkbox_match++;
					if(k == 1 && _array[i]['vegetarian'] == 1)
						checkbox_match++;
					if(k == 2 && _array[i]['takeaway'] == 1)
						checkbox_match++;
					if(k == 3 && _array[i]['alcohol'] == 1)
						checkbox_match++;
				}
			}
			if(checkbox_checksum == 0 || (checkbox_match == checkbox_checksum && checkbox_match != 0))
				$('#browse_restaurants_body').append('<div class="dynamic_restaurant_link" id="'+_array[i]["name"]+'" style="cursor: hand;">'+_array[i]["name"]+'</div>');
		}
	//	alert("checksum:" + checkbox_checksum);
		//alert("checkbox_match:"+checkbox_match);
	
		//if(parseInt(checkbox_checks) == 0 || (checkbox_match == checkbox_checksum && checkbox_match != 0))
			//checkbox_matched = true;

		/*if(search_text == true && checkbox_matched == true)
		{
			//form_data[i] = _array[i];
			$('#browse_restaurants_body').append('<div class="dynamic_restaurant_link" id="'+_array[i]["name"]+'" style="cursor: hand;">'+_array[i]["name"]+'</div>');
		}*/
	}
}

function remove_div(_id)
{
	var divEls = document.getElementsByTagName("div");
	for(var i = 0; i < divEls.length; i++)
	{
		if(divEls[i].id == _id){
			divEls[i].remove();
			return;
		}
	}
}


//flickr images
$('#btn_flickr').bind('click', function(){
	var my_window = add_window(500,500,"Restaurant Pictures");
	$(my_window).css("left", "50");
	$(my_window).css("top", "50");
	$(my_window).append("<div id='temp_text'>Loading Images...</div>");

    var geocoder;
    geocoder = new google.maps.Geocoder();
      
	var address = restaurant_address;
	//set_latitude(123);
	
	geocoder.geocode( { 'address': address}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {

			var form_data = {
					x: results[0].geometry.location.lat(),
					y: results[0].geometry.location.lng(),
					size: 2
				};
				
				$.ajax({
			           url: "<?php echo site_url('flickr/get_images');?>",
					   type: 'POST',
					   data: form_data,
			           
			           success: function (msg) {
			        	   $("#temp_text").remove();
			        	 	//view the new page
			        	 	var newDiv = $(document.createElement('div'))
							.attr("id","flickr_images");
						
							//add some style to the div
							$(newDiv).css('background-color', '#223');
							$(newDiv).css('width', 490);
							$(newDiv).css('height', 470);
							$(newDiv).css('position', 'absolute');
							$(newDiv).css('left', 5);
							$(newDiv).css('top', 25);
						
							//append it to the _div
							newDiv.appendTo(my_window);
			       			
							$(newDiv).append(msg);
							
			           }
			         });
		}
	});
	
	
		return false;
});


//google maps
$('#btn_gm').bind('click', function(){
	var my_window = add_window(500,500,"Google Map");
	$(my_window).css("left", "50");
	$(my_window).css("top", "50");
	initialize_google_map(restaurant_address,$(my_window));
    
	return false;
});

//open recommend-to-user dialog
$('#btn_recommend_user').bind('click', function(){
	$.ajax({
       url: "<?php echo site_url('account/get_recommendation_page');?>",
	   type: 'POST',
	   //dataType: 'json',
	   data: {restaurant_name: restaurant_name},
       
       success: function (msg) {
    	 	//view the new page
   			var my_window = add_window(500,240,"Recommendation");
   			$(my_window).css("left", "50");
   			$(my_window).css("top", "50");
			//$(my_window).append(msg.javascript);
			$(my_window).append(msg);
       }
     });
	
	return false;
});


$('.dynamic_restaurant_link').live('click', function() {

	restaurant_name = this.getAttribute('id');
	restaurant_address = this.getAttribute('address');
	
	var form_data = {
			name: restaurant_name
	};

	$.ajax({
		url: '<?php echo site_url("restaurant/dynamic_page");?>',
		type: 'POST',
		data: form_data,
		success: function(msg) {
			//$('#browse_restaurants_body').add('div').addClass('browse_restaurants_page');
			//$('#browse_restaurants_body').append('<div class="browse_restaurants_page"></div>');
			//$('#browse_restaurants_body').empty();
			//$('.dynamic_restaurant_link').remove();
			$('#search_box').hide(fade_delay);
			$('#browse_restaurants_body').empty();
			$('#browse_restaurants_body').append(msg);
		}
	});
	return false;
});


$('#btn_back').click(function(){
	$('#search_box').show(fade_delay);
	update_restaurant_list();
    
	return false;
});

$('#btn_review').click(function(){
	var form_data = {
		restaurant_name : restaurant_name,
		message : $('#review').val()
	};

	$.ajax({
		url: "<?php echo site_url('restaurant/review');?>",
		data: form_data,
  		type: 'POST',
        
        success: function (msg) {
    		if(msg == "error")
    		{
    			_text = "Invalid format.";
    			_color = "orange";

    		}
    		else
    		{
    			_text = "Review added!";
    			_color = "green";
        	}
        	$("#review_response_display").empty();
    		$("#review_response_display").append("<div style='color: "+_color+";'>"+_text+"</div>");
        }
      });
    
	return false;
});

$('#btn_edit').click(function(){
	var form_data = {
			name: restaurant_name
		};

	$.ajax({
		url: '<?php echo site_url("restaurant/edit");?>',
		type: 'POST',
		data: form_data,
		success: function(msg) {
			$('#browse_restaurants_body').empty();
			$('#browse_restaurants_body').append(msg);
		}
	});

	return false;
});


$('#btn_favorite_list').click(function(){
	$.ajax({
		url: '<?php echo site_url("restaurant/favorite_list");?>',
		type: 'POST',
        success: function (msg) {
			$('#browse_restaurants_body').empty();
			$('#browse_restaurants_body').append(msg);
			}
    });
	return false;
});


$('#btn_add_to_favorite').click(function(){
	var form_data = {
		name: restaurant_name
	};

	$.ajax({
		url: "<?php echo site_url('restaurant/add_to_favorite');?>",
		type: 'POST',
        data: form_data,
		
        success: function (msg) {
		var _text = "error";
		var _color = "red";
		if(msg != "ok")
		{
			_text = "It is already added to your favorites.";
			_color = "orange";
		}
		else
		{
			_text = restaurant_name + "was added to your favorites!";
			_color = "green";

		}
			$("#review_response_display").empty();
			$("#review_response_display").append("<div style='color: "+_color+";'>"+_text+"</div>");
		}
    });
		return false;
});


$('#submit_edit').click(function(){
	
	var form_data = {
		name: restaurant_name,
		address_street: $('#address_street').val(),
		address_zipcode: $('#address_zipcode').val(),
		address_city: $('#address_city').val(),
		opening_hours: $('#opening_hours').val(),
		kitchen: $('#kitchen').val(),
		fastfood: $('#fastfood').val(),
		vegetarian: $('#vegetarian').val(),
		alcohol: $('#alcohol').val(),
		takeaway: $('#takeaway').val()
	};

	$.ajax({
		url: "<?php echo site_url('restaurant/check_edit_restaurant_form');?>",
		type: 'POST',
        data: form_data,
		
        success: function (msg) {
		var _text = "error";
		var _color = "red";
		if(msg != "ok_streetok_zipcodeok_cityok_openok_kitchen")
		{
			_text = "Invalid format.";
			_color = "orange";
		}
		else
		{
			_text = "Restaurant information changed!";
			_color = "green";

		}
			$("#submit_restaurant_display").empty();
			$("#submit_restaurant_display").append("<div style='color: "+_color+";'>"+_text+"</div>");
		}
    });
		return false;
});


$('input#search_text').keyup(function () {
	update_restaurant_list();

	return false;
});


//checkboxes
$('#search_cb_fastfood').change(function() {
	if($('#search_cb_fastfood').val() == 0)
		$('#search_cb_fastfood').val(1)
	else
		$('#search_cb_fastfood').val(0)

	update_restaurant_list();
	return false;
});
$('#search_cb_vegetarian').change(function () {
	if($('#search_cb_vegetarian').val() == 0)
		$('#search_cb_vegetarian').val(1)
	else
		$('#search_cb_vegetarian').val(0)

	update_restaurant_list();
	return false;
});

$('#search_cb_takeaway').change(function () {
	if($('#search_cb_takeaway').val() == 0)
		$('#search_cb_takeaway').val(1)
	else
		$('#search_cb_takeaway').val(0)
	update_restaurant_list();
	return false;
});
$('#search_cb_alcohol').change(function () {
	if($('#search_cb_alcohol').val() == 0)
		$('#search_cb_alcohol').val(1)
	else
		$('#search_cb_alcohol').val(0)
	update_restaurant_list();
	return false;
});

</script>